<template>
  <div class="login-container">
    <van-nav-bar
      class="page-nav-bar"
      title="登录"
    >
    <template #left>
     <van-icon class="close_btn" name="cross" @click="$router.back()"/>
    </template>
    </van-nav-bar>
    <van-form
      @submit="onSubmit"
      ref="loginForm"
    >
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        type="number"
        maxlength="11"
        :rules="userFormRules.mobile"
      >
        <i
          slot="left-icon"
          class="toutiao toutiao-shouji"
        ></i>
      </van-field>
      <van-field
        v-model="user.code"
        type="number"
        name="code"
        placeholder="请输入密码"
        maxlength="6"
        :rules="userFormRules.code"
      >
        <i
          slot="left-icon"
          class="toutiao toutiao-yanzhengma"
        ></i>
        <template
          #button
          v-if="isCountDownShow"
        >
          <van-count-down
            format=" ss s"
            :time="1000 * 5"
            @finish="isCountDownShow=false"
          />
        </template>

        <template
          #button
          v-else
        >
          <van-button
            size="small"
            type="default"
            round
            class="send-sms-btn"
            native-type="button"
            @click="sendCode"
          >发送验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button
          class="login-btn"
          block
          type="info"
          native-type="submit"
          @click="onLogin"
        >
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, getSmsCode } from '@/api/user'
export default {
  name: 'LoginPage',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      },
      isCountDownShow: false
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    async onSubmit () {
      console.log(this.user)
      try {
        const res = await login(this.user)
        console.log('登录成功' + res)
      } catch (err) {
        console.log('登录失败', err)
      }
    },
    async onLogin () {
      this.$toast.loading({
        duration: 0, // 持续时间，0表示持续展示不停止
        forbidClick: true, // 是否禁止背景点击
        message: '登录中...' // 提示消息
      })

      try {
        const res = await login(this.user)
        console.log('登录成功', res)
        // 提示 success 或者 fail 的时候，会先把其它的 toast 先清除
        this.$store.commit('setUser', res.data.data)
        this.$toast.success('登录成功')
        this.$router.push('./my')
      } catch (err) {
        console.log('登录失败', err)
        this.$toast.fail('登录失败，手机号或验证码错误')
      }
    },
    async sendCode () {
      // console.log('sendCode')
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch (err) {
        return console.log(err)
      }
      // 2验证通过 倒计时显示
      this.isCountDownShow = true

      // 3请求发送验证码
      // 3. 请求发送验证码
      try {
        await getSmsCode(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        // 发送失败，关闭倒计时
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  .close_btn{
    color: #fff;
    font-size: 35px;
  }
  i.toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    // width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    color: #666;
    font-size: 22px;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
      border-radius: 15px;
    }
  }
}
</style>
